#####################################
### R Function: calibrate.samford ###
#####################################

calibrate.samford=function(x){
  repdata.cal=matrix(NA, nrow=nrow(raw.data), ncol=1)
  repdata.cal=as.data.frame(repdata.cal)
  
  #raplib
  raw=raw.data$raplib
  deviation=raw-x[2, "raplib"]
  scalar=NA
  for(i in 1:length(deviation)){
    if(deviation[i]>=0){
      scalar[i]=3/(x[3, "raplib"]-x[2, "raplib"])    
    } else{
      scalar[i]=-3/(x[1, "raplib"]-x[2, "raplib"])      
    } 
  }
  product=deviation*scalar
  exponent=exp(product)
  membership=exponent/(1+exponent)
  repdata.cal["raplib"]=membership
  
  #manufac
  raw=raw.data$manufac
  deviation=raw-x[2, "manufac"]
  scalar=NA
  for(i in 1:length(deviation)){
    if(deviation[i]>=0){
      scalar[i]=3/(x[3, "manufac"]-x[2, "manufac"])    
    } else{
      scalar[i]=-3/(x[1, "manufac"]-x[2, "manufac"])      
    } 
  }
  product=deviation*scalar
  exponent=exp(product)
  membership=exponent/(1+exponent)
  repdata.cal["manufac"]=membership
  
  #devalu
  raw=raw.data$devalu
  deviation=raw-x[2, "devalu"]
  scalar=NA
  for(i in 1:length(deviation)){
    if(deviation[i]>=0){
      scalar[i]=3/(x[3, "devalu"]-x[2, "devalu"])    
    } else{
      scalar[i]=-3/(x[1, "devalu"]-x[2, "devalu"])      
    } 
  }
  product=deviation*scalar
  exponent=exp(product)
  membership=exponent/(1+exponent)
  repdata.cal["devalu"]=membership
  
  #hyperinf
  raw=raw.data$hyperinf
  deviation=raw-x[2, "hyperinf"]
  scalar=NA
  for(i in 1:length(deviation)){
    if(deviation[i]>=0){
      scalar[i]=3/(x[3, "hyperinf"]-x[2, "hyperinf"])    
    } else{
      scalar[i]=-3/(x[1, "hyperinf"]-x[2, "hyperinf"])      
    } 
  }
  product=deviation*scalar
  exponent=exp(product)
  membership=exponent/(1+exponent)
  repdata.cal["hyperinf"]=membership
  
  #grostron
  raw=raw.data$grostron
  deviation=raw-x[2, "grostron"]
  scalar=NA
  for(i in 1:length(deviation)){
    if(deviation[i]>=0){
      scalar[i]=3/(x[3, "grostron"]-x[2, "grostron"])    
    } else{
      scalar[i]=-3/(x[1, "grostron"]-x[2, "grostron"])      
    } 
  }
  product=deviation*scalar
  exponent=exp(product)
  membership=exponent/(1+exponent)
  repdata.cal["grostron"]=membership
  
  #groweak
  raw=raw.data$groweak
  deviation=raw-x[2, "groweak"]
  scalar=NA
  for(i in 1:length(deviation)){
    if(deviation[i]>=0){
      scalar[i]=3/(x[3, "groweak"]-x[2, "groweak"])    
    } else{
      scalar[i]=-3/(x[1, "groweak"]-x[2, "groweak"])      
    } 
  }
  product=deviation*scalar
  exponent=exp(product)
  membership=exponent/(1+exponent)
  repdata.cal["groweak"]=membership
  
  #switcher
  raw=raw.data$switcher
  deviation=raw-x[2, "switcher"]
  scalar=NA
  for(i in 1:length(deviation)){
    if(deviation[i]>=0){
      scalar[i]=3/(x[3, "switcher"]-x[2, "switcher"])    
    } else{
      scalar[i]=-3/(x[1, "switcher"]-x[2, "switcher"])      
    } 
  }
  product=deviation*scalar
  exponent=exp(product)
  membership=exponent/(1+exponent)
  repdata.cal["switcher"]=membership
  
  #execunco
  raw=raw.data$execunco
  deviation=raw-x[2, "execunco"]
  scalar=NA
  for(i in 1:length(deviation)){
    if(deviation[i]>=0){
      scalar[i]=3/(x[3, "execunco"]-x[2, "execunco"])    
    } else{
      scalar[i]=-3/(x[1, "execunco"]-x[2, "execunco"])      
    } 
  }
  product=deviation*scalar
  exponent=exp(product)
  membership=exponent/(1+exponent)
  repdata.cal["execunco"]=membership
  
  repdata.cal=repdata.cal[,-1]
  repdata.cal[is.na(repdata.cal)==TRUE]<-1
  
  return(repdata.cal)
  
}
  




